﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using SIPO.Library.DataAccess.Report;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Web.Support;
using Microsoft.Reporting.WebForms;

namespace SIPO.Web.Reports.Transaction
{
    public partial class rptIncome : BasePage
    {
       
           protected override void OnLoad(EventArgs e)
           {
               recTransactionTypeRepo recTransactionTypeDal = new recTransactionTypeRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
               rptPenerimaanRepo penerimaanDal = new rptPenerimaanRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
               string parameter = string.Empty;

               if (!IsPostBack)
               {
                   //populate dropdownlist transactionType
                   List<recTransactionType> transTypes = recTransactionTypeDal.GetList();

                   ddlJenisPungutan.Items.Clear();
                   if (transTypes.Count > 0)
                   {
                       transTypes.ForEach(delegate(recTransactionType transType)
                       {
                           ddlJenisPungutan.Items.Add(new ListItem(transType.TransTypeCode + "-" + transType.TransTypeName, transType.TransTypeID.ToString()));
                       });
                   }
                   ddlJenisPungutan.Items.Insert(0, new ListItem("-- jenis pungutan --", string.Empty));

                   if (Context.Request.Params["PeriodeAwal"] != null || Context.Request.Params["PeriodeAkhir"] != null || Context.Request.Params["SectorID"] != null)
                   {
                       string path = string.Empty;

                       DateTime? periodeAwal = Context.Request.Params["PeriodeAwal"] == "" ? (DateTime?)null : Helper.FormatDate(Context.Request.Params["PeriodeAwal"], Helper.Culture.Indonesian);
                       DateTime? periodeAkhir = Context.Request.Params["PeriodeAkhir"] == "" ? (DateTime?)null : Helper.FormatDate(Context.Request.Params["PeriodeAkhir"], Helper.Culture.Indonesian);
                       string sectorID = Context.Request.Params["SectorID"] == "" ? string.Empty : Context.Request["SectorID"].ToString();
                       string subSectorID = Context.Request.Params["SubSectorID"] == "" ? string.Empty : Context.Request["SubSectorID"].ToString();
                       string subSubSectorID = Context.Request.Params["SubSubSectorID"] == "" ? string.Empty : Context.Request["SubSubSectorID"].ToString();
                       string transTypeID = Context.Request.Params["TransTypeID"] == "" ? string.Empty : Context.Request.Params["TransTypeID"].ToString();
                       short? detail = Context.Request.Params["Detail"] == "" ? (short?)0 : short.Parse(Context.Request.Params["Detail"].ToString());

                       //konversi tanggal
                       string sPeriodeAwal = string.Format("{0:yyyyMMdd}", periodeAwal);
                       string sPeriodeAkhir = string.Format("{0:yyyyMMdd}", periodeAkhir);

                       //isi parameter
                       if (!sectorID.Equals(string.Empty))
                       {
                           parameter += "where recSector.SectorID in(" + sectorID + ")";

                           if (!subSectorID.Equals(string.Empty))
                           {
                               parameter += " and recSubSector.SubSectorID in(" + subSectorID + ")";

                               if (!subSubSectorID.Equals(string.Empty))
                               {
                                   parameter += " and recSubSubSector.SubSubSectorID in(" + subSubSectorID + ")";
                               }
                           }

                           if (!sPeriodeAwal.Equals(string.Empty) && !sPeriodeAkhir.Equals(string.Empty))
                           {
                               parameter += " and recInvoice.InvoiceDate between '" + sPeriodeAwal + "' and '" + sPeriodeAkhir + "'";
                           }

                           if (!transTypeID.Equals(string.Empty))
                           {
                               parameter += " and recTransactionType.TransTypeID= '" + transTypeID + "'";
                           }
                       }
                       else if (transTypeID.Equals(string.Empty))
                       {
                           if (!sPeriodeAwal.Equals(string.Empty) && !sPeriodeAkhir.Equals(string.Empty))
                           {
                               parameter += "where recInvoice.InvoiceDate between '" + sPeriodeAwal + "' and '" + sPeriodeAkhir + "'";
                           }
                       }
                       else
                       {
                           parameter += "where recTransactionType.TransTypeID= '" + transTypeID + "'";

                           if (!sPeriodeAwal.Equals(string.Empty) && !sPeriodeAkhir.Equals(string.Empty))
                           {
                               parameter += " and recInvoice.InvoiceDate between '" + sPeriodeAwal + "' and '" + sPeriodeAkhir + "'";
                           }
                       }

                       try
                       {
                           DataTable dt = penerimaanDal.GetPenerimaan(parameter);
                           rsViewer.Reset();
                           path = detail == 1 ? "~/reportviewer/Transaction/rptIncomeDetail.rdlc" : "~/reportviewer/Transaction/rptIncome.rdlc";
                           rsViewer.LocalReport.ReportPath = Server.MapPath(path);
                           ReportDataSource rds = new ReportDataSource();
                           rds.Name = "DataSet1";
                           rds.Value = dt;

                           rsViewer.LocalReport.DataSources.Clear();
                           rsViewer.LocalReport.DataSources.Add(rds);
                           rsViewer.LocalReport.Refresh();
                       }
                       catch (Exception ex)
                       {
                           Response.Write(ex.Message);
                       }
                   }
               }
           }
           
      
    }
}